package com.lbz.converter;

import com.lbz.dao.CategoryMapper;
import com.lbz.entity.Category;
import com.lbz.entity.Product;
import com.lbz.utils.DateTimeUtil;
import com.lbz.utils.PropertiesUtil;
import com.lbz.vo.ProductDetailVO;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 将product转换成ProductDetailVO
 *
 * @Author 林必昭
 * @Date 2019/6/15 21:45
 */

public class Product2ProductDetailVOConverter {

    @Autowired
    private static CategoryMapper categoryMapper;


    public static ProductDetailVO convert(Product product){

        ProductDetailVO productDetailVO = new ProductDetailVO();

        productDetailVO.setId(product.getId());
        productDetailVO.setCategoryId(product.getCategoryId());
        productDetailVO.setDetail(product.getDetail());
        productDetailVO.setMainImage(product.getMainImage());
        productDetailVO.setName(product.getName());
        productDetailVO.setPrice(product.getPrice());
        productDetailVO.setStock(product.getStock());
        productDetailVO.setStatus(product.getStatus());
        productDetailVO.setSubImages(product.getSubImages());
        productDetailVO.setSubtitle(product.getSubtitle());

        productDetailVO.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix=","http://img.happymlbz.com/"));

        productDetailVO.setCreateTime(DateTimeUtil.dateToStr(product.getCreateTime()));
        productDetailVO.setUpdateTime(DateTimeUtil.dateToStr(product.getUpdateTime()));

        Category category = categoryMapper.selectByPrimaryKey(product.getCategoryId());
        if (category==null){
            productDetailVO.setParentCategoryId(0); //默认为根节点
        }else {
            productDetailVO.setParentCategoryId(category.getParentId());
        }
        return productDetailVO;
    }
}
